package io.renren.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.dao.SchoolDao;
import io.renren.entity.SchoolEntity;
import io.renren.util.LocationUtils;
import io.renren.service.SchoolService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("schooolService")
public class SchoolServiceImpl extends ServiceImpl<SchoolDao, SchoolEntity> implements SchoolService {

    /**
     * 小程序
     * 获取学校详情
     *
     * @param schoolId  学校id
     * @param longitude 地理经度
     * @param latitude  地理纬度
     * @return R
     */
    @Override
    public R getSchoolDetail(Integer schoolId, double longitude, double latitude) {
        //获取学校星星总数
        Integer star = baseMapper.getAllStar(schoolId);
        //获取该学校的评论数量
        Integer schoolCount = baseMapper.getSchoolCount(schoolId);
        double a = star / schoolCount / 4;
        //保留一位小数点
        double avgStar = new BigDecimal(a).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        //获取学校图库
        List<Map<String, Object>> getSchoolPicList = baseMapper.getSchoolPic(schoolId);
        //获取学校详情
        Map<String, Object> getSchoolDetail = baseMapper.getSchoolDetail(schoolId);
        //获取该学校的科目
        List<Map<String, Object>> getSchoolSubjectList = baseMapper.getSchoolSubjectList(schoolId);
        //在学校详情中获取经纬度，并转为double(目标经纬度)
        String longitudeM = getSchoolDetail.get("longitude").toString();
        String latitudeM = getSchoolDetail.get("latitude").toString();
        Double longitudeM1 = Double.parseDouble(longitudeM);
        Double latitudeM1 = Double.parseDouble(latitudeM);
        //计算出距离
        Double a1 = LocationUtils.getDistance(latitude, longitude, latitudeM1, longitudeM1);
        double distance = Math.floor(a1);
        getSchoolDetail.put("distance", distance / 1000);
        Map<String, Object> schoolDetail = new HashMap<>();
        schoolDetail.put("schoolPic", getSchoolPicList);
        schoolDetail.put("schoolDetail", getSchoolDetail);
        schoolDetail.put("schoolSubject", getSchoolSubjectList);
        schoolDetail.put("star", avgStar);
        return R.ok(schoolDetail);
    }

    /***
     *小程序
     *获取须知
     * @param type 1须知2关于我们
     * @return R
     */
    @Override
    public R getNotice(Integer type) {
        List<Map<String, Object>> notice = baseMapper.getNotice(type);
        return R.ok(notice);
    }

    /***
     * 小程序
     * 获取城市
     * @return R
     */
    @Override
    public R getCity() {
        List<Map<String, Object>> city = baseMapper.getCity();
       /* String arr[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};*/
        return R.ok(city);
    }

    /***
     * 小程序
     * 获取热门值学校
     * @param city 城市
     * @return
     */
    @Override
    public R getHotCity(String city) {
        List<Map<String, Object>> hotCity = baseMapper.getHotCity(city);
        return R.ok(hotCity);
    }

    /***
     *  小程序
     *  根据筛选条件选出学校
     * @param studentId 学员
     * @param city 城市名
     * @param location 位置距离(1顺序排序(近) 2倒序(远))
     * @param evaluate 评价星（1顺序 少 2倒序 多）
     * @param type 科目类别 1人社 2交通 3安监
     * @param price 科目价格最低价(1顺序价格低 2倒序价格高)
     * @param schoolName 学校名称
     * @param longitude 地理经度
     * @param latitude 地理纬度
     * @return
     */
    @Override
    public R getSchool(String city, Integer location, Integer evaluate, Integer type, Integer price, String schoolName, Long studentId, double longitude, double latitude) {
        //根据筛选条件选出学校
        List<Map<String, Object>> school = baseMapper.getSchool(city, location, evaluate, type, price, schoolName, longitude, latitude);
        //搜索表新增数据
        baseMapper.insertSearch(schoolName, studentId);
        return R.ok(school);
    }

    /***
     * 获取学校评价
     * @param schoolId 学校id
     * @return
     */
    @Override
    public R getSchoolEvaluate(Integer schoolId) {
        List<Map<String,Object>>getSchoolEvaluateList=baseMapper.getSchoolEvaluate(schoolId);
        return R.ok(getSchoolEvaluateList);
    }
    /***
     * 获取学校名称
     * @param schoolId 学校id
     * @return
     */
    @Override
    public Map<String, Object> getSchoolName(Integer schoolId) {
        Map<String,Object>getSchoolName=baseMapper.getSchoolName(schoolId);
        return getSchoolName;
    }
}
